<template>
  <div class="profile-container">
    <div class="profile-header">
      <h1>个人中心</h1>
    </div>
    
    <div class="profile-card">
      <div class="user-info">
        <div class="avatar">
          <i class="iconfont icon-user"></i>
        </div>
        <div class="user-details">
          <h2>{{ user.name }}</h2>
          <p>{{ user.email }}</p>
          <span class="role-badge">{{ user.role === 'admin' ? '管理员' : '普通用户' }}</span>
        </div>
      </div>
      
      <div class="action-buttons">
        <button @click="showChangePassword = true" class="action-btn">
          <i class="iconfont icon-lock"></i>
          修改密码
        </button>
        <button @click="handleLogout" class="action-btn logout-btn">
          <i class="iconfont icon-logout"></i>
          退出登录
        </button>
      </div>
    </div>

    <!-- 修改密码弹窗 -->
    <div v-if="showChangePassword" class="modal-overlay" @click="showChangePassword = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>修改密码</h3>
          <button @click="showChangePassword = false" class="close-btn">
            <i class="iconfont icon-close"></i>
          </button>
        </div>
        
        <form @submit.prevent="handleChangePassword" class="password-form">
          <div class="input-group">
            <span class="input-icon"><i class="iconfont icon-lock"></i></span>
            <input 
              :type="showOldPwd ? 'text' : 'password'" 
              v-model="passwordForm.oldPassword" 
              placeholder="请输入原密码" 
              required 
              class="input" 
            />
            <span class="eye-icon" @click="showOldPwd = !showOldPwd">
              <i class="iconfont" :class="showOldPwd ? 'icon-eye' : 'icon-eye-close'"></i>
            </span>
          </div>
          
          <div class="input-group">
            <span class="input-icon"><i class="iconfont icon-lock"></i></span>
            <input 
              :type="showNewPwd ? 'text' : 'password'" 
              v-model="passwordForm.newPassword" 
              placeholder="请输入新密码" 
              required 
              class="input" 
            />
            <span class="eye-icon" @click="showNewPwd = !showNewPwd">
              <i class="iconfont" :class="showNewPwd ? 'icon-eye' : 'icon-eye-close'"></i>
            </span>
          </div>
          
          <div class="input-group">
            <span class="input-icon"><i class="iconfont icon-lock"></i></span>
            <input 
              :type="showConfirmPwd ? 'text' : 'password'" 
              v-model="passwordForm.confirmPassword" 
              placeholder="请确认新密码" 
              required 
              class="input" 
            />
            <span class="eye-icon" @click="showConfirmPwd = !showConfirmPwd">
              <i class="iconfont" :class="showConfirmPwd ? 'icon-eye' : 'icon-eye-close'"></i>
            </span>
          </div>
          
          <div class="form-actions">
            <button type="button" @click="showChangePassword = false" class="cancel-btn">取消</button>
            <button type="submit" :disabled="!isPasswordValid || loading" class="submit-btn">
              {{ loading ? '修改中...' : '确认修改' }}
            </button>
          </div>
          
          <p v-if="passwordError" class="error">{{ passwordError }}</p>
          <p v-if="passwordSuccess" class="success">{{ passwordSuccess }}</p>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import { userAPI, apiUtils } from '@/services/api';

export default {
  name: 'ProfilePage',
  data() {
    return {
      user: {},
      showChangePassword: false,
      showOldPwd: false,
      showNewPwd: false,
      showConfirmPwd: false,
      passwordForm: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      loading: false,
      passwordError: '',
      passwordSuccess: ''
    };
  },
  computed: {
    isPasswordValid() {
      return this.passwordForm.oldPassword && 
             this.passwordForm.newPassword && 
             this.passwordForm.confirmPassword &&
             this.passwordForm.newPassword === this.passwordForm.confirmPassword &&
             this.passwordForm.newPassword.length >= 6;
    }
  },
  mounted() {
    this.loadUserInfo();
  },
  methods: {
    loadUserInfo() {
      const user = apiUtils.getCurrentUser();
      if (user) {
        this.user = user;
      } else {
        this.$router.push('/login');
      }
    },
    
    async handleChangePassword() {
      if (!this.isPasswordValid) return;
      
      this.loading = true;
      this.passwordError = '';
      this.passwordSuccess = '';
      
      try {
        const data = await userAPI.changePassword(
          this.user.username,
          this.passwordForm.oldPassword,
          this.passwordForm.newPassword
        );
        
        if (data.success) {
          this.passwordSuccess = '密码修改成功！';
          this.passwordForm = {
            oldPassword: '',
            newPassword: '',
            confirmPassword: ''
          };
          
          // 3秒后关闭弹窗
          setTimeout(() => {
            this.showChangePassword = false;
            this.passwordSuccess = '';
          }, 3000);
        } else {
          this.passwordError = data.error || '密码修改失败';
        }
      } catch (e) {
        this.passwordError = e.message || '网络错误，请稍后重试';
      } finally {
        this.loading = false;
      }
    },
    
    handleLogout() {
      if (confirm('确定要退出登录吗？')) {
        apiUtils.logout();
        this.$router.push('/login');
      }
    }
  }
};
</script>

<style scoped>
@import url('//at.alicdn.com/t/c/font_4244842_1v7v6k2k2k.css');

.profile-container {
  min-height: 100vh;
  background: #f5f5f5;
  padding: 20px;
}

.profile-header {
  text-align: center;
  margin-bottom: 30px;
}

.profile-header h1 {
  font-size: 2rem;
  color: #333;
  margin: 0;
}

.profile-card {
  background: #fff;
  border-radius: 16px;
  padding: 30px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.user-info {
  display: flex;
  align-items: center;
  margin-bottom: 30px;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: #9ee6b0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 20px;
}

.avatar i {
  font-size: 40px;
  color: #fff;
}

.user-details h2 {
  margin: 0 0 8px 0;
  font-size: 1.5rem;
  color: #333;
}

.user-details p {
  margin: 0 0 8px 0;
  color: #666;
  font-size: 1rem;
}

.role-badge {
  background: #9ee6b0;
  color: #fff;
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 0.9rem;
}

.action-buttons {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.action-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 15px;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s;
}

.action-btn:first-child {
  background: #9ee6b0;
  color: #fff;
}

.action-btn:first-child:hover {
  background: #7edc97;
}

.logout-btn {
  background: #ff6b6b;
  color: #fff;
}

.logout-btn:hover {
  background: #ff5252;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: #fff;
  border-radius: 16px;
  padding: 30px;
  width: 90%;
  max-width: 400px;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.3rem;
  color: #333;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: #999;
  cursor: pointer;
}

.input-group {
  display: flex;
  align-items: center;
  background: #f6f7fa;
  border-radius: 12px;
  margin-bottom: 20px;
  padding: 0 15px;
  border: 1px solid #f0f0f0;
  height: 50px;
}

.input-icon {
  color: #bbb;
  font-size: 18px;
  margin-right: 10px;
}

.input {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 16px;
  padding: 12px 0;
  outline: none;
}

.eye-icon {
  color: #bbb;
  font-size: 18px;
  cursor: pointer;
  margin-left: 10px;
}

.form-actions {
  display: flex;
  gap: 15px;
  margin-top: 20px;
}

.cancel-btn, .submit-btn {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.3s;
}

.cancel-btn {
  background: #f0f0f0;
  color: #666;
}

.cancel-btn:hover {
  background: #e0e0e0;
}

.submit-btn {
  background: #9ee6b0;
  color: #fff;
}

.submit-btn:hover:not(:disabled) {
  background: #7edc97;
}

.submit-btn:disabled {
  background: #e0e0e0;
  cursor: not-allowed;
}

.error {
  color: #e74c3c;
  margin-top: 10px;
  font-size: 14px;
  text-align: center;
}

.success {
  color: #4caf50;
  margin-top: 10px;
  font-size: 14px;
  text-align: center;
}
</style> 